package org.ran.demo.http.http;

import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

import org.ran.demo.http.util.FileUtil;
import org.ran.demo.http.util.StreamUtil;
/**
 * 把请求写入磁盘
 * @author caoran
 *
 */
public class SaveRequestServer {
    public static void main(String[] args) throws Exception {
        try (ServerSocket serverSocket = new ServerSocket();) {
            serverSocket.bind(new InetSocketAddress(8080));
            Socket socket = serverSocket.accept();
            InputStream in = socket.getInputStream();
            List<String> lines = new ArrayList<>();
            while (true) {
                String request = StreamUtil.readLine(in);
                lines.add(request);
                if(request.isEmpty()) {
                    break;
                }
            }
            Files.write(Paths.get(FileUtil.REQUEST_LOCATION), lines, StandardOpenOption.CREATE);
            socket.close();
        } catch (Exception e) {
        }
    }
}
